<?php
namespace app\houtai\controller\system;
use app\houtai\controller\BaseController;

class PlatformController extends BaseController {
  /**
   *  @brief  : 平台综合业绩
   *  @return : page
   *  @details: 
   */
  public function reportAction() {
    $list = [];
    $CHANNELS = $this->GetAdminChannel();
    ////////////////////////////////////////////////////////////////////////////
    $LimitChannel = empty($CHANNELS) ? [] : array_keys($CHANNELS);
    $LimitChannel = empty($LimitChannel) ? '' : implode(',', $LimitChannel);
    $LimitChannel = empty($LimitChannel) ? '' : "WHERE m.Channel IN({$LimitChannel})";
    $TIMES = [
      'D'   => [],// 合计
      'D0'  => ['today', 'tomorrow'],  // 'today'// 今日
      'D1'  => ['yesterday', 'today'], // 'yesterday'// 昨天
      'D7'  => ['this week 00:00:00', 'next week 00:00:00'],  // 'week'// 本周
      'D14' => ['last week 00:00:00', 'this week 00:00:00'], // 'last week'// 上周
      'D30' => ['first Day of this month 00:00:00', 'first Day of next month 00:00:00'], // 'month'// 本月
      'D60' => ['first Day of last month 00:00:00', 'first Day of this month 00:00:00'],  // 'last month'// 上月
    ];
    $sqls = [];
    foreach($TIMES as $key=>$val) {
      $where = '';
      if (!empty($val)) {
        $a = date('Y-m-d H:i:s', strtotime($val[0]));
        $b = date('Y-m-d H:i:s', strtotime($val[1]));
        $where = "AND (a.Addtime BETWEEN '{$a}' AND '{$b}')";
      }
      $sql = "(SELECT '{$key}' AS t, m.Channel, ISNULL(m.a, 0) AS a, ISNULL(n.b, 0) AS b FROM ( SELECT c.Channel, SUM (c.RMB) AS a FROM ( SELECT b.Channel, a.MerBillNo, SUM (DISTINCT a.RMB) AS RMB FROM dbo.ChargeOnline AS a LEFT JOIN CYBasicDB.dbo.AccountsInfo b ON b.UserID = a.UserID WHERE a.Status = 1 AND a.GameStatus = 1 {$where} GROUP BY b.Channel, a.MerBillNo ) c GROUP BY c.Channel ) m FULL OUTER JOIN ( SELECT b.Channel, SUM (a.myRebate) b FROM dbo.AgentChargeRebate AS a LEFT JOIN AgentReg b ON b.UserID = a.AgentID WHERE a.IsDisable = 0 {$where} GROUP BY b.Channel ) n ON m.Channel = n.Channel {$LimitChannel})";
      $sqls[] = $sql;
    }
    $sqls = implode(' UNION ALL ', $sqls);
    $list = db('', 'rydb.websms')->query($sqls);
    ////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', ['CHANNELS' => $CHANNELS, 'list' => $list]);
  }
}
